package com.itheima.health.service.impl;

import com.itheima.health.mapper.BusinessMapper;
import com.itheima.health.plus.Setmealplus;
import com.itheima.health.service.BusinessService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.poi.MainPoi1;
import com.itheima.health.utils.poi.POIUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public Map findTableData() {
        //固定表格的大小9*4，n*4

        /** Excel 文件要存放的位置，假定在D盘下*/
        String outputFile = "D:\\test.xls";




        Map map = new HashMap();
        Date reportDate=DateUtils.getToday();
        map.put("reportDate",reportDate);//报告日期                不传值（当前系统时间）

        int todayNewMember=businessMapper.findNewMember(reportDate);
        map.put("todayNewMember",todayNewMember);//今日新增会员数        以今天日期为条件查询会员数

        int totalMember=businessMapper.findAllMember();
        map.put("totalMember",totalMember);//总会员数                 查询所有会员数

        Date weekone=DateUtils.getFirstDayOfWeek(reportDate);
        int thisWeekNewMember=businessMapper.findNewMemberOnThisWeek(weekone,reportDate);
        map.put("thisWeekNewMember",thisWeekNewMember);//本周新增会员数      查会员表用between本周时间

        Date monthone = DateUtils.getFirstDayDateOfMonth(reportDate);
        int thisMonthNewMember=businessMapper.findNewMemberOnThisMonth(monthone,reportDate);
        map.put("thisMonthNewMember",thisMonthNewMember);//本月新增会员数     查会员表用between本月时间

        int todayOrderNumber=businessMapper.findTodayOrderNumber(reportDate);
        map.put("todayOrderNumber",todayOrderNumber);//今日预约数           以今天日期为条件查询预约数

        int todayVisitsNumber=businessMapper.findTodayVisitsNumber(reportDate);
        map.put("todayVisitsNumber",todayVisitsNumber);//今日到诊数          以今天日期为条件查询到诊数

        int thisWeekOrderNumber=businessMapper.findThisWeekOrderNumber(weekone,reportDate);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);//本周预约数        查预约表用between本周时间

        int thisWeekVisitsNumber=businessMapper.findThisWeekVisitsNumber(weekone,reportDate);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);//本周到诊数       查预约表用between本周时间

        int thisMonthOrderNumber=businessMapper.findthisMonthOrderNumber(monthone,reportDate);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);//本月预约数       查预约表用between本月时间

        int thisMonthVisditsNumber=businessMapper.findthisMonthVisditsNumber(monthone,reportDate);
        map.put("thisMonthVisditsNumber",thisMonthVisditsNumber);//本月到诊数      查预约表用between本月时间

        List<Map<String,Object>> hotSetmeal=businessMapper.findHotSetmeal();
        //查询所有订单数（套餐）
        int sumSetmeal=businessMapper.findSumSetmeal();
        System.out.println(hotSetmeal.toString());

        try {
            for (Map<String, Object> stringObjectMap : hotSetmeal) {

                stringObjectMap.put("proportion",((Long)stringObjectMap.get("setmeal_count")).doubleValue()/(double) sumSetmeal*100);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

        map.put("hotSetmeal",hotSetmeal);//热门套餐                  查询套餐表以套餐分组，把得出的list集合按从大到小排序

        POIUtils.createXlsx(map);
        return map;
    }
}
